<?php
if(!session_id()) { session_start(); }
if(!isset($_SESSION['user_id'])) {
	header('Location: index.php');
}
include_once('functions/main_functions.php');
	page_head();
?>
	<div id="main">
		<div id="content">
			<h2>Option Price Calculator</h2>
			<?php
			include_once('classes/portfolio.class.php');
			include_once('classes/stock.class.php');
			$portfolio = new Portfolio();
			$calculatorType = $portfolio->getCalculatorType();
			if($calculatorType == 0) {
				echo "<h3>Binomial</h3>";
				echo '<form id="calculator-form" onsubmit="add_portfolio(); return false;">';
			} else {
				echo "<h3>Black Scholes</h3>";
				echo '<form id="calculator-form" onsubmit="add_portfolio_black(); return false;">';
			}
			?>
			
				<table>
					<tbody>
						<tr><td colspan='2'><input type="radio" value="0" checked="" name="call_put">Call</td></tr>
						<tr><td colspan='2'><input type="radio" value="1" name="call_put">Put</td></tr>
						<tr><td>Stock Name</td>
						<td>
							<select id="stock_name" name="stock_name">
								<option value="">--</option>
								<?php
								$stocks = new Stock();
								$results = $stocks->ListStocks();
								foreach($results as $item) {
									echo '<option value="' . $item['stock_id'] . '">' . $item['stock_name'] . '</option>';
								}
								?>
							</select>
						</td></tr>
						<tr><td>Stock Price</td><td><input readonly="readonly" id="stock_price" type="text" name="stock_price" size="5"></td></tr>
						<tr><td>Strike Price Of Option</td><td><input id="option_price" type="text" name="option_price" size="5"></td></tr>
						<tr><td>Time to Expiration (Expiry rate) [in years]</td><td><input id="expiry_time" type="text" name="expiry" size="5"></td></tr>
						<tr><td>Risk Free Interest Rate (e.g. 0.02 for 2%)</td><td><input id="interest_rate" type="text" name="interest_rate" size="5">%</td></tr>
						<tr><td>Volatility (e.g. 0.25 for 25%) </td><td><input id="volatility" type="text" name="volatility" size="5">%</td></tr>
					</tbody>
				</table>
				<input type="submit" value="Calculate" name="submit">
			</form>
		</div>
		<table id="calculator-results">
			<tr>
				<td>Stock Name</td>
				<td>Stock Price</td>
				<td>Option Price</td>
				<td>Expiry Date</td>
				<td>Interest</td>
				<td>Volatility</td>
				<td>Result</td>
			</tr>
				<td><span id="stock-name"></span></td>
				<td><span id="stock-price"></span></td>
				<td><span id="option-price"></span></td>
				<td><span id="expiry"></span></td>
				<td><span id="interest"></span></td>
				<td><span id="volatility"></span></td>
				<td><span id="final-result"></span></td>
			</tr>
		</table>
<?php
	echo '</div>';
//function to get the cumulative normal distribution of a z-score
	page_foot();
?>